package com.syni.mdd.yhd.common.all.service.api;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.config.AuroraConfig;
import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dto.requestDto.api.PushDto;
import com.syni.mdd.yhd.common.all.entity.BmsDxNews;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsDxNewsService;
import com.syni.mdd.yhd.common.all.service.BmsVideoService;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import com.syni.mdd.yhd.common.all.utils.aurora.AuroraPushUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiAuroraPush.CommonPushDto;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @className AuroraPushService
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/17 9:08
 * @VERSION 1.0
 **/
@Service
@Log4j2
public class AuroraPushService {

    @Autowired
    BmsDxNewsService bmsDxNewsService;

    @Autowired
    BmsVideoService bmsVideoService;

    public Map<String,Object> pushNews(PushDto pushDto) {
        Optional<BmsDxNews> bmsDxNewsOptional = bmsDxNewsService.getBaseDao().findById(pushDto.getNewsId());
        if(bmsDxNewsOptional.isPresent()){
            BmsDxNews bmsDxNews = bmsDxNewsOptional.get();
            //处理系统消息
            if(bmsDxNews.getParentType() == Constants.PARENTTYPT_SYS){
                handParentTypeSys(bmsDxNews);
            }
            //进行极光推送
            return ResultMapHelper.success();
        }else {
            return ResultMapHelper.failure("数据丢失");
        }

    }

    /**
     * @Description 处理系统消息
     * @Param BmsDxNews
     * @Author: cfx
     * @Date: 2019/8/20
     */
    private void handParentTypeSys(BmsDxNews bmsDxNews) {
        //处理视频审核
        String code = null;
        String alert = null;
        Map<Integer,String> funMap = new HashMap<>();
        //用户发布视频审核
        funMap.put(Constants.PARENTTYPT_SYS_USER_VIDEO_AUDIT,"handSysNewsByUserVideo");
        //店铺视频审核
        funMap.put(Constants.PARENTTYPT_SYS_BUS_VIDEO,"handSysNewsByBusinessVideo");
        //店员视频审核
        funMap.put(Constants.PARENTTYPT_SYS_ASS_REA_VIDEO,"handSysNewsByAssistantVideo");
        //店铺审核
        funMap.put(Constants.PARENTTYPT_SYS_BUSINESS_AUDIT,"handSysNewsByBusinessAuth");
        //店铺下架
        funMap.put(Constants.PARENTTYPT_SYS_BUSINESS_UNLOAD,"handSysNewsByBusinessUpload");
        //店铺报错
        funMap.put(Constants.PARENTTYPT_SYS_BUSINESS_ERROR,"handSysNewsByBusinessReportError");
        //店铺团购审核
        funMap.put(Constants.PARENTTYPT_SYS_BUSINESS_GROUP_BUY_APPROVE,"handSysNewsByBusinessGroupAudit");
        //视频被后台强制下线
        funMap.put(Constants.PARENTTYPT_SYS_VIDEO_ADMIN_OFF,"handSysNewsByVideoForcedOffline");
        //团购被后台强制下线
        funMap.put(Constants.PARENTTYPT_SYS_GROUP_BUY_ADMIN_OFF,"handSysNewsByGroupBuyOffline");
        //买单被后台强制下线
        funMap.put(Constants.PARENTTYPT_SYS_COUPON_ADMIN_OFF,"handSysNewsByCouponOffline");
        //群发被后台强制下线
        funMap.put(Constants.PARENTTYPT_SYS_MESSAGE_ADMIN_OFF,"handSysNewsByGroupMessageDelete");
        //银行卡确认
        funMap.put(Constants.PARENTTYPT_SYS_BANK_COMFIRM,"handSysNewsByBankConfirm");
        //支付宝确认
        funMap.put(Constants.PARENTTYPT_SYS_PAY_COMFIRM,"handSysNewsByPayConfirm");
        //二维码裂变
        funMap.put(Constants.PARENTTYPT_SYS_CODE_PRO,"handSysNewsByCodePro");
        //分销通知
        funMap.put(Constants.FX_INFORM,"handFxInform");
        if(funMap.containsKey(bmsDxNews.getChildType())){
            String funName = funMap.get(bmsDxNews.getChildType());
            try {
                Method method = this.getClass().getMethod(funName,BmsDxNews.class);
                method.invoke(this,bmsDxNews);
            } catch (Exception e) {
                log.error("pushError",e);
            }
        }
       /* if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_USER_VIDEO_AUDIT){
            //用户发布视频审核
            handSysNewsByUserVideo(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_BUS_VIDEO){
            //店铺视频审核
            handSysNewsByBusinessVideo(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_ASS_REA_VIDEO){
            //店员视频审核
            handSysNewsByAssistantVideo(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_BUSINESS_AUDIT){
            //店铺审核
            handSysNewsByBusinessAuth(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_BUSINESS_ERROR){
            //店铺报错
            handSysNewsByBusinessReportError(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_BUSINESS_GROUP_BUY_APPROVE){
            //店铺团购审核
            handSysNewsByBusinessGroupAudit(bmsDxNews);
        } else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_VIDEO_ADMIN_OFF){
            //视频被后台强制下线
            handSysNewsByVideoForcedOffline(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_GROUP_BUY_ADMIN_OFF){
            //团购被后台强制下线
            handSysNewsByGroupBuyOffline(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_COUPON_ADMIN_OFF){
            //买单被后台强制下线
            handSysNewsByCouponOffline(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_MESSAGE_ADMIN_OFF){
            //群发被后台强制下线
            handSysNewsByGroupMessageDelete(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_BANK_COMFIRM){
            //银行卡确认
            handSysNewsByBankConfirm(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_PAY_COMFIRM){
            //支付宝确认
            handSysNewsByPayConfirm(bmsDxNews);
        }else if(bmsDxNews.getChildType() == Constants.PARENTTYPT_SYS_CODE_PRO){
            handSysNewsByCodePro(bmsDxNews);
        }*/
        /*if(code != null && alert != null) {
            BmsCommonService.pushToFront(bmsDxNews, code, alert);
        }*/

    }

    public void handFxInform(BmsDxNews bmsDxNews){
        String code = AuroraPushCode.USER_FX_INFO;
        Map<String,Object> messageBody = JSON.parseObject(bmsDxNews.getContent());
        messageBody.put("code",code);
        String userAlias;
        if(bmsDxNews.getOriginRole() == Constants.BUSINESS_ROLE_ID){
            userAlias = AuroraConfig.BUSINESS_PREFIX + bmsDxNews.getBmsBusinessId().toString();
        }else if(bmsDxNews.getOriginRole() == Constants.ASSISTANT_ROLE_ID){
            userAlias = AuroraConfig.BUSINESS_ASS_PREFIX + bmsDxNews.getBmsUserId().toString();
        }else {
            userAlias = AuroraConfig.USER_PREFIX + bmsDxNews.getBmsUserId().toString();
        }
        String[] audAlias = {userAlias};
        Map<String,String> messageBodyTurn = new HashMap<>();
        messageBody.forEach((key,value) -> {
            messageBodyTurn.put(key,value.toString());
        });
        BmsCommonService.pushCustomMessageToFront(messageBodyTurn,audAlias,bmsDxNews.getOriginRole());
    }

    public void handSysNewsByCodePro(BmsDxNews bmsDxNews) {
        String code = AuroraPushCode.USER_RECE_PRO_CODE;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert());
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    /**
     * @Description 支付宝消息确认
     * @Author: cfx
     * @Date: 2019/9/18
     */
    public void handSysNewsByPayConfirm(BmsDxNews bmsDxNews) {
        String code = AuroraPushCode.BUSINESS_PAY_CONFIRM;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert());
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    /**
     * @Description 处理系统消息_银行卡确认
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByBankConfirm(BmsDxNews bmsDxNews) {
        String code = AuroraPushCode.BUSINESS_BANK_CONFIRM;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert());
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    /**
     * @Description 处理系统消息_群发被强制删除
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByGroupMessageDelete(BmsDxNews bmsDxNews) {
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String releaseTime = jsonNewsBody.get("releaseTime").toString();
        String releaseNum = jsonNewsBody.get("releaseNum").toString();
        String messageDesc = jsonNewsBody.get("messageDesc").toString();
        String deleteReason = jsonNewsBody.get("deleteReason").toString();
        String code = AuroraPushCode.BUSINESS_MESSAGE_DELETE;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),releaseTime,releaseNum,messageDesc,deleteReason);
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    public void handSysNewsByCouponOffline(BmsDxNews bmsDxNews) {
        //买单被后台强制下线
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String commitTime = jsonNewsBody.get("commitTime").toString();
        String payContent = jsonNewsBody.get("payContent").toString();
        String offlineReason = jsonNewsBody.get("offlineReason").toString();
        String code = AuroraPushCode.BUSINESS_COUPON_OFFINE;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),commitTime,payContent,offlineReason);
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    public void handSysNewsByGroupBuyOffline(BmsDxNews bmsDxNews) {
        //视频被后台强制下线
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String commitTime = jsonNewsBody.get("commitTime").toString();
        String groupBuyName = jsonNewsBody.get("groupBuyName").toString();
        String offlineReason = jsonNewsBody.get("offlineReason").toString();
        String code = AuroraPushCode.BUSINESS_GROUPBUY_OFFINE;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),commitTime,groupBuyName,offlineReason);
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    /**
     * @Description 处理系统消息_视频被强制下线
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByVideoForcedOffline(BmsDxNews bmsDxNews) {
        //视频被后台强制下线
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String videoReleaseTime = jsonNewsBody.get("videoReleaseTime").toString();
        String videoDesc = jsonNewsBody.get("videoDesc").toString();
        String offlineReason = jsonNewsBody.get("offlineReason").toString();
        String code = AuroraPushCode.VIDEO_MAN_OFFLINE;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),videoReleaseTime,videoDesc,offlineReason);
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    /**
     * @Description 处理系统消息_团购审核
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByBusinessGroupAudit(BmsDxNews bmsDxNews) {
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String commitTime = jsonNewsBody.get("commitTime").toString();
        String groupBuyName = jsonNewsBody.get("groupBuyName").toString();
        String code = AuroraPushCode.BUSINESS_GROUPBUY_AUDIT_SUCCESS;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),commitTime,groupBuyName);
        if(bmsDxNews.getStatus() == Constants.COMMON_FLAG_FALSE_INT){
            String noPassReason = jsonNewsBody.get("noPassReason").toString();
            code = AuroraPushCode.BUSINESS_GROUPBUY_AUDIT_FAIL;
            alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),commitTime,groupBuyName,noPassReason);
        }
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    public void handSysNewsByBusinessReportError(BmsDxNews bmsDxNews) {
        String code = AuroraPushCode.BUSINESS_REPORT_ERROR;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),bmsDxNews.getContent());
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    /**
     * @Description 处理系统消息_店铺资料审核
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByBusinessUpload(BmsDxNews bmsDxNews) {
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String offlineReason = jsonNewsBody.get("offlineReason").toString();
        String code = AuroraPushCode.BUSINESS_FORCE_OFFLINE;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),offlineReason);
        BmsCommonService.pushToFront(bmsDxNews,code,alert);
    }

    /**
     * @Description 处理系统消息_店铺资料审核
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByBusinessAuth(BmsDxNews bmsDxNews) {
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String commitTime = jsonNewsBody.get("commitTime").toString();
        String code = AuroraPushCode.BUSINESS_AUDIT_SUCCESS;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),commitTime);
        if(bmsDxNews.getStatus() == Constants.COMMON_FLAG_FALSE_INT){
            String noPassReason = jsonNewsBody.get("noPassReason").toString();
            code = AuroraPushCode.BUSINESS_AUDIT_FAIL;
            alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),commitTime,noPassReason);
        }
        BmsCommonService.pushToFront(bmsDxNews,code,alert);
    }


    /**
     * @Description 处理系统消息_店员视频审核
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByAssistantVideo(BmsDxNews bmsDxNews) {
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String videoReleaseTime = jsonNewsBody.get("videoReleaseTime").toString();
        String videoDesc = jsonNewsBody.get("videoDesc").toString();
        String code = AuroraPushCode.ASSISTANT_VIDEO_SUCCESS;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),videoReleaseTime,
                videoDesc);
        if(bmsDxNews.getStatus() == Constants.COMMON_FLAG_FALSE_INT){
            String offlineReason = jsonNewsBody.get("offlineReason").toString();
            code = AuroraPushCode.ASSISTANT_VIDEO_FAIL;
            alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),videoReleaseTime,videoDesc,offlineReason);
        }
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    /**
     * @Description 处理系统消息_用户视频审核
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByUserVideo(BmsDxNews bmsDxNews) {
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String videoReleaseTime = jsonNewsBody.get("videoReleaseTime").toString();
        String videoDesc = jsonNewsBody.get("videoDesc").toString();
        String code = AuroraPushCode.VIDEO_AUDIT_SUCCESS;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),videoReleaseTime,
                videoDesc);
        if(bmsDxNews.getStatus() == Constants.COMMON_FLAG_FALSE_INT){
            String offlineReason = jsonNewsBody.get("offlineReason").toString();
            code = AuroraPushCode.VIDEO_AUDIT_FAIL;
            alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),videoReleaseTime,videoDesc,offlineReason);
        }
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }

    /**
     * @Description 处理系统消息_店铺视频审核
     * @Author: cfx
     * @Date: 2019/9/3
     */
    public void handSysNewsByBusinessVideo(BmsDxNews bmsDxNews) {
        Map<String,Object> jsonNewsBody = (Map<String, Object>) JSON.parse(bmsDxNews.getContent());
        String videoReleaseTime = jsonNewsBody.get("videoReleaseTime").toString();
        String videoDesc = jsonNewsBody.get("videoDesc").toString();
        String code = AuroraPushCode.BUSINESS_VIDEO_SUCCESS;
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),videoReleaseTime,
                videoDesc);
        if(bmsDxNews.getStatus() == Constants.COMMON_FLAG_FALSE_INT){
            String offlineReason = jsonNewsBody.get("offlineReason").toString();
            code = AuroraPushCode.BUSINESS_VIDEO_FAIL;
            alert = String.format(AuroraPushCode.pushTemplateMap.get(code).getAlert(),videoReleaseTime,videoDesc,offlineReason);
        }
        BmsCommonService.pushToFront(bmsDxNews, code, alert);
    }


    public void commonPushNews(CommonPushDto commonPushDto) {
        List<String> alias = JSON.parseArray(commonPushDto.getAlias(),String.class);
        String[] userAlias = alias.toArray(new String[alias.size()]);
        Map<String,String> messageBody =  (Map<String, String>) JSON.parse(commonPushDto.getContent());
        if(StringUtils.isNotBlank(commonPushDto.getCode())){
            messageBody.put("code",commonPushDto.getCode());
        }
        AuroraPushUtils.sendMessageToAppByAlias(AuroraPushUtils.ALL,userAlias,messageBody,
                commonPushDto.getAlert(),commonPushDto.getRoleId());
    }


    public void commonCustomMessage(CommonPushDto commonPushDto) {
        AuroraPushUtils.pushCommonCustomMessage(commonPushDto);
    }
}
